package dao.Supper.impl;

import dao.BaseDao;
import dao.Supper.SuperCustomerDao;
import pojo.Customer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 顾客实现类
 */
public class SuperCustomerDaoImpl extends BaseDao implements SuperCustomerDao {
    /**
     * 整合查询
     * @param sql
     * @param params
     * @return
     */
    public List<Customer>  QueryCus(String sql,Object[] params){
        List<Customer> list = new ArrayList<>();
        ResultSet rs = this.executeSelect(sql,params);
        try {
            while (rs.next()) {
                Customer customer = new Customer();
                customer.setId(rs.getLong("id"));
                customer.setCusBirthday(rs.getTimestamp("cusBirthday"));
                customer.setCusCode(rs.getString("cusCode"));
                customer.setCusEmail(rs.getString("cusEmail"));
                customer.setCusHobby(rs.getString("cusHobby"));
                customer.setCusName(rs.getString("cusName"));
                customer.setCusLoginName(rs.getString("cusLoginName"));
                customer.setCusPassword(rs.getString("cusPassword"));
                customer.setCusSex(rs.getString("cusSex"));
                customer.setCusPhoto(rs.getString("cusPhoto"));
                list.add(customer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return list;
    }


    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    public List<Customer> selCusByID(Long id) {
        String sql = "select * from customer where id=?";
        Object[] params = {id};
        return QueryCus(sql,params);
    }

    /**
     * 得到总数据行
     * @param id
     * @param cusName
     * @param cusSex
     * @return
     */
    public int getTotalCount(Long id, String cusName, String cusSex) {
        int res = 0;
        try {
            StringBuffer sql = new StringBuffer("SELECT count(*) FROM `customer` WHERE 1=1");
            if (id > 0) {
                sql.append(" and id=" + id);
            }
            if (cusName != null && cusName != "") {
                sql.append(" and cusName like " + "\'%" + cusName + "%\'");
            }
            if (cusSex != null && cusSex != "") {
                sql.append(" and cusSex =" + "\'" + cusSex + "\'");
            }
            ResultSet rs = this.executeSelect(sql.toString(), null);

            if (rs != null) {
                while (rs.next()) {
                    res = rs.getInt(1);
                }
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeAll();
        }
        return res;
    }

    /**
     * 显示顾客信息
     * @param currentPage
     * @param pageSize
     * @param id
     * @param cusName
     * @param cusSex
     * @return
     */
    public List<Customer> selCustomer(int currentPage, int pageSize, Long id, String cusName, String cusSex) {
        StringBuffer sql = new StringBuffer("SELECT * FROM `customer` WHERE 1=1");
        if (id > 0) {
            sql.append(" and id=" + id);
        }
        if (cusName != null && cusName != "") {
            sql.append(" and cusName like " + "\'%" + cusName + "%\'");
        }
        if (cusSex != null && cusSex != "") {
            sql.append(" and cusSex =" + "\'" + cusSex + "\'");
        }
        sql.append(" limit ?,?");
        Object[] params = {(currentPage-1)*pageSize,pageSize};
        return QueryCus(sql.toString(),params);

    }

    /**
     * 修改
     * @param customer
     * @return
     */
    public int updateScus(Customer customer) {
        int res = 0;
        try {
            String sql = "UPDATE `customer` SET `cusName`=?,`cusLoginName`=?,`cusEmail`=?,`cusSex`=?,`cusHobby`=?,`cusCode`=?,`cusBirthday`=? WHERE `id`=?";
            Object[] params = {customer.getCusName(), customer.getCusLoginName(), customer.getCusEmail(), customer.getCusSex(), customer.getCusHobby(), customer.getCusCode(), customer.getCusBirthday(), customer.getId()};
            res = this.executeUpdateRows(sql,params);
        }finally {
            this.closeAll();
        }
        return res;
    }

    /**
     * 根据id进行删除
     * @param id
     * @return
     */
    public int delScus(Long id) {
        int res = 0;
        try {
            String sql = "delete from `customer` where `id`=?";
            Object[] params = {id};
            res = this.executeUpdateRows(sql,params);
        }finally {
            this.closeAll();
        }
        return res;
    }
}
